/*
 * @Author: 恒恒 1067531094@qq.com
 * @Date: 2022-10-11 16:49:34
 * @LastEditors: 恒恒 1067531094@qq.com
 * @LastEditTime: 2022-10-11 20:55:18
 * @FilePath: \作业\Demo.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// 导入模块
const fs = require('fs')
const path = require('path')
const express = require('express')
const bodyparser = require('body-parser')
const app = new express()

app.use(express.urlencoded({ extended: false }))

//渲染登录页面

app.get('/login', (req, res) => {
    res.writeHead(200, { "content-type": "text/html;charset=utf-8" });
    let pathurl = path.join(__dirname, '/login.html')
    let data = fs.readFileSync(pathurl)
    res.end(data)
})

app.post("/login", (req, res) => {
    let { name, age, sex } = req.body;
    if (name == '梁志恒') {
        res.send("姓名已存在")
    } else {
        res.send("姓名可以注册")
    }
})

//渲染注册页面

app.get('/reg', (req, res) => {
    res.writeHead(200, { "content-type": "text/html;charset=utf-8" });
    let pathurl = path.join(__dirname, '/reg.html')
    let data = fs.readFileSync(pathurl)
    res.end(data)
})

app.post("/reg", (req, res) => {
    let { name, age, sex } = req.body;
    if (name == '梁志恒') {
        res.send("已注册")
    } else {
        res.send("可以注册")
    }
})


// 设置监听端口
app.listen(3000, () => {
    console.log('服务器启动成功')
})